<template>
  <a-drawer :title="title" :maskClosable="false" @close="onDrawerClose" :visible="visible" :width="600">
    <template v-if="visible">
      <a-table
        ref="table"
        :columns="columns"
        :rowKey="(row, index) => row.Id"
        :dataSource="data"
        :loading="loading"
        @change="handleTableChange"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, getCheckboxProps: PropsDisabled }"
        :bordered="true"
        size="small"
      >
        <!-- //:pagination="pagination" -->
      </a-table>
    </template>
    <div style="height: 30px"></div>
    <div
      style="
        position: absolute;
        bottom: 0;
        padding: 5px;
        text-align: right;
        width: 92%;
        border-top: 1px solid #ccc;
        background-color: #fff;
      "
    >
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="hanldleAdd()" :loading="iconLoading">保存</a-button>
      </div>
    </div>
  </a-drawer>
</template>

<script>
const columns = [{ title: '组织名称', dataIndex: 'Text' }]

export default {
  props: {
    parentObj: Object
  },
  mounted() {
    this.getDataList()
  },
  data() {
    return {
      list: [],
      iconLoading: false,
      visible: false,
      title: '',
      b: '',
      userId: '',
      data: [],
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRowKeys: []
    }
  },
  methods: {
    PropsDisabled(record) {
      let DepartmentID = this.list.map(item => {
        return item.DepartmentID
      })
      let Value = DepartmentID.includes(record.Value)
      return {
        props: {
          disabled: Value
        }
      }
    },
    handleTableChange(filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    getDataList() {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/Base_Manage/Base_Department/GetTreeDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          const pagination = { ...this.pagination }
          pagination.total = resJson.Total
          this.pagination = pagination
        })
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    hanldleAdd() {
      this.iconLoading = true
      this.$http
        .post('/Base_Manage/Base_UserDepartment/SaveData', { UserID: this.userId, DepartmentIds: this.selectedRowKeys })
        .then(resJson => {
          if (resJson.Success) {
            this.$message.success('操作成功!')
            //this.afterSubmit()
            this.visible = false
            this.selectedRowKeys = []
            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
          setTimeout(() => {
            this.iconLoading = false
          }, 600)
        })
    },
    openDrawer(id, row) {
      this.title = '添加组织'
      this.userId = id
      this.visible = true
      this.list = row
      // console.log(this.list)
      // console.log(this.data)
      this.parentObj.getDataList()
      // this.selectedRowKeys = this.parentObj.data.map(item => item.DepartmentID)
    },
    onDrawerClose() {
      this.visible = false
    }
  }
}
</script>
